<?php
/**
* usermark.php
*
* Create on 2016-9-6
* Create by TT
*/

namespace haibao\user\business;

class UserMark extends \haibao\user\business\Base{

	/**
	 * 收藏图片列表
	 * @param unknown_type $query
	 */
	public static function getMarkImageList($query){
		$data = new \haibao\user\data\UserMark();
		return $data->getAll($query);
	}
	
	/**
	 * 添加图片收藏
	 * @param \haibao\user\model\data\markimage $model
	 */
	public static function addMarkImage($model){
		$data = new \haibao\user\data\UserMark();
		\haibao\user\common\ScoreRule::operateScore($model->UserId,\haibao\user\common\ScoreRule::TYPE_MARK);
		return $data->addMarkImage($model);
	}
	/**
	 * 是否收藏
	 * @param \haibao\user\model\data\markimage $model
	 * @param unknown $imageIdArr
	 */
	public static function hasMarkImage($imageIdArr,$userId){
	    $data = new \haibao\user\data\UserMark();
	    return $data->hasMarkImage($imageIdArr,$userId);
	}
	/**
	 * 删除收藏图片
	 * @param int $userId
	 * @param array/int $imageIds
	 */
	public static function delMarkImages($userId,$imageIds){
		$data = new \haibao\user\data\UserMark();
		$data->delMarkImages($userId, $imageIds);
	}
	
	/**
	 * 修改收藏图片分类
	 * @param int $userId
	 * @param int $categoryId
	 * @param array/int $imageIds
	 */
	public static function updateMarkImageCatetory($userId,$categoryId,$imageIds){
		$data = new \haibao\user\data\UserMark();
		$data->updateMarkImageCatetory($userId, $categoryId, $imageIds);
	}
	
	/**
	 * 修改收藏图片描述
	 * @param int $userId
	 * @param int $imageId
	 * @param string $desc
	 */
	public static function updateMarkImageDesc($userId,$imageId,$desc){
		$data = new \haibao\user\data\UserMark();
		$data->updateMarkImageDesc($userId, $imageId, $desc);
	}
	
	/**
	 * 用户收藏分类列表
	 * @param int $userId
	 */
	public static function getMarkCategoryList($userId){
		$data = new \haibao\user\data\UserMark();
		return $data->getMarkCategoryList($userId);
	}

	public static function getUserMarkImageIds($userId){
		$data = new \haibao\user\data\UserMark();
		return $data->getUserMarkImageIds($userId);
	}
	/**
	 * 添加收藏分类
	 * @param \haibao\user\model\data\markcategory $model
	 */
	public static function addMarkCategory($model){
		$return = 1;
		$data = new \haibao\user\data\UserMark();
		if(mb_strlen($model->CategoryName) > 15){
			$return = 2;
		}else{
			//判断分类名是否已经存在
			$isHav = $data->havCategoryName($model->UserId,$model->CategoryName);
			if($isHav){
				//异常
				$return = 0;
			}
			$data->add($model);
		}
		return $return;
	}


	public static function delMarkCategory($categoryId,$userId){
		$data = new \haibao\user\data\UserMark();
		$data->delMarkCategory($categoryId,$userId);
	}

	public static function updateMarkCategory($categoryId,$categoryName,$userId){
		$return = 1;
		$data = new \haibao\user\data\UserMark();
		if(mb_strlen($categoryName) > 15){
			$return = 2;
		}else{
			$isHav = $data->havCategoryName($userId,$categoryName,$categoryId);
			if($isHav){
				$return = 0;
			}
			$data->updateMarkCategory($categoryId,$categoryName,$userId);
		}
		return $return;
	}

	public static function havCategoryName($userId,$categoryName,$categoryId = null){
		$data = new \haibao\user\data\UserMark();
		$isHav = $data->havCategoryName($userId,$categoryName,$categoryId);
		return $isHav ? 1 : 0;
	}

	public static function manageCategory($userId,$categoryInfo){
		$addInfos = array();
		if($categoryInfo) {
			$models = $delIds = $updateCates = array();
			foreach ($categoryInfo as $data) {
				if($data['id'] == 0){
					$model = new \haibao\user\model\data\MarkCategory();
					$model->CategoryName = $data['name'];
					$model->UserId = $userId;
					$models[] = $model;
				}
				if ($data['id'] != 0 && $data['name']) {
					$updateCates[] = array($data['id'], $data['name']);
				}
				if($data['id'] != 0 && $data['name'] == ''){
					$delIds[] = $data['id'];
				}
			}
			$data = new \haibao\user\data\UserMark();
			$data->manageCategory($models, $updateCates, $delIds, $userId);
			$cates = $data->getMarkCategoryList($userId);
			foreach($cates as $data){
				$addInfos[$data['CategoryName']] = $data['Id'];
			}
		}

		return $addInfos;
	}

	public static function getImageIdsByUserId($userId,$categoryId=-1,$searchName=''){
		$data = new \haibao\user\data\UserMark();
		return $data->getImageIdsByUserId($userId,$categoryId,$searchName);
	}

	public static function getMarkImagesByImageIds($userId,$imageIds){
		$data = new \haibao\user\data\UserMark();
		return $data->getMarkImagesByImageIds($userId,$imageIds);
	}


}